# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import time
import re


requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告


"""
Airbnb爱彼迎
https://www.airbnb.cn/
"""



class Airbnb_Get():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        self.cookie = cookiejar.CookieJar()
        ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            # "User-Agent": ua.random
            # 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.1 Safari/605.1.15'
            'authority': 'www.airbnb.cn',
            'method': 'GET',
            'path': '/api/v2/explore_tabs?_format=for_explore_search_web&auto_ib=true&client_session_id=13739987-f99c-4c32-89ab-138a98a212fe&currency=CNY&current_tab_id=home_tab&experiences_per_grid=20&federated_search_session_id=0683dab8-2995-44ad-b0b1-32d0bdaee787&fetch_filters=true&guidebooks_per_grid=20&has_zero_guest_treatment=true&hide_dates_and_guests_filters=false&is_guided_search=true&is_new_cards_experiment=true&is_standard_search=true&items_offset=40&items_per_grid=20&key=d306zoyjsyarp7ifhu67rjxn52tv0t20&locale=zh&metadata_only=false&place_id=ChIJhZpmtO7WFDQR_kdS9hU4BHw&query=%E6%B3%89%E5%B7%9E%E9%B2%A4%E5%9F%8E%E5%8C%BA&query_understanding_enabled=true&refinement_paths%5B%5D=%2Fhomes&s_tag=8Q2uLfYw&satori_version=1.1.13&screen_height=376&screen_size=large&screen_width=1536&section_offset=4&selected_tab_id=home_tab&show_groupings=true&supports_for_you_v3=true&timezone_offset=480&version=1.7.3',
            'scheme': 'https',
            'accept': 'application/json, text/javascript, */*; q=0.01',
            'accept-encoding': 'gzip, deflate, br',
            'accept-language': 'zh-CN,zh;q=0.9',
            'cookie': 'bev=1583131149_MTM0OTIwNDkwNDE2; _csrf_token=V4%24.airbnb.cn%24H_IWVuQbH9M%24ObIT8EUTpqyACkl2qwmLGlsdPf0uAjR5UdSH2w3zgmc%3D; jitney_client_session_id=13739987-f99c-4c32-89ab-138a98a212fe; jitney_client_session_created_at=1583131149; _user_attributes=%7B%22curr%22%3A%22CNY%22%2C%22guest_exchange%22%3A6.991899999999999%2C%22device_profiling_session_id%22%3A%221583131149--caa1d3fe5808951e5063ca28%22%2C%22giftcard_profiling_session_id%22%3A%221583131149--bd549e6adfaf630af91fd115%22%2C%22reservation_profiling_session_id%22%3A%221583131149--db0ab6a514db3b351900c3c0%22%7D; __xsptplusUT_840=1; cfrmfctr=DESKTOP; cbkp=4; __xsptplus840=840.1.1583131153.1583131269.2%234%7C%7C%7C%7C%7C%23%23qdgGLdH37lLTDbZVzHlUOcIWFrnUQDW9%23; jitney_client_session_updated_at=1583131329; flags=268697600',
            'dnt': '1',
            'referer': 'https://www.airbnb.cn/s/%E6%B3%89%E5%B7%9E%E9%B2%A4%E5%9F%8E%E5%8C%BA/homes?refinement_paths%5B%5D=%2Fhomes&current_tab_id=home_tab&selected_tab_id=home_tab&screen_size=large&hide_dates_and_guests_filters=false&place_id=ChIJhZpmtO7WFDQR_kdS9hU4BHw&s_tag=8Q2uLfYw&section_offset=4&items_offset=40',
            'sec-fetch-dest': 'empty',
            'sec-fetch-mode': 'cors',
            'sec-fetch-site': 'same-origin',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36',
            'x-csrf-token': 'V4$.airbnb.cn$H_IWVuQbH9M$ObIT8EUTpqyACkl2qwmLGlsdPf0uAjR5UdSH2w3zgmc=',
            'x-csrf-without-token': '1',
            'x-requested-with': 'XMLHttpRequest'


        }


    def get_contents(self, current):
        postData = {
            '_format': 'for_explore_search_web',
            'auto_ib': 'true',
            'client_session_id': '13739987-f99c-4c32-89ab-138a98a212fe',
            'currency': 'CNY',
            'current_tab_id': 'home_tab',
            'experiences_per_grid': '20',
            'federated_search_session_id': '0683dab8-2995-44ad-b0b1-32d0bdaee787',
            'fetch_filters': 'true',
            'guidebooks_per_grid': '20',
            'has_zero_guest_treatment': 'true',
            'hide_dates_and_guests_filters': 'false',
            'is_guided_search': 'true',
            'is_new_cards_experiment': 'true',
            'is_standard_search': 'true',

            'items_per_grid': '20',
            'key': 'd306zoyjsyarp7ifhu67rjxn52tv0t20',
            'locale': 'zh',
            'metadata_only': 'false',
            'place_id': 'ChIJhZpmtO7WFDQR_kdS9hU4BHw',
            'query': '泉州鲤城区',
            'query_understanding_enabled': 'true',
            'refinement_paths[]': '/homes',
            's_tag': '8Q2uLfYw',
            'satori_version': '1.1.13',
            'screen_height': '376',
            'screen_size': 'large',
            'screen_width': '1536',
            'section_offset': '4',
            'selected_tab_id': 'home_tab',
            'show_groupings': 'true',
            'supports_for_you_v3': 'true',
            'timezone_offset': '480',
            'version': '1.7.3'
        }
        # 第一页无此参数 游标
        if current != 0:
            postData['items_offset'] = str(current),
        url = f"https://www.airbnb.cn/api/v2/explore_tabs"
        html = HttpUtils.do_request("GET", url, self.headers, postData)
        data_json = json.loads(html.text)
        for d in data_json['explore_tabs'][0]['sections'][0]['listings']:
            dict_data = dict()
            dict_data['id'] = d['listing']['id']  # id
            dict_data['url'] = f"https://www.airbnb.cn/rooms/{dict_data['id']}"
            dict_data['name'] = d['listing']['name']  # 名字
            dict_data['space_type'] = d['listing']['space_type']  # 类型
            dict_data['bedrooms'] = d['listing']['bedrooms']  # 房间数量
            dict_data['bathrooms'] = d['listing']['bathrooms']  # 卫生间数量
            dict_data['beds'] = d['listing']['beds']  # 床数量
            dict_data['lat'] = d['listing']['lat']  # lat
            dict_data['lng'] = d['listing']['lng']  # lng
            dict_data['star_rating'] = d['listing']['star_rating']  # 评分
            dict_data['preview_tags'] = ""  # 标签
            for p in d['listing']['preview_tags']:
                dict_data['preview_tags'] += "|" + p['name']
            dict_data['price'] = d['pricing_quote']['price_string']
            print(str(dict_data))


if __name__ == '__main__':
    airbnb_get = Airbnb_Get()
    for i in range(0, 100, 20):
        print(i)
        airbnb_get.get_contents(i)